package com.lanou3g.bookstore.user.service;

import com.lanou3g.bookstore.user.dao.UserDao;
import com.lanou3g.bookstore.user.domain.User;
import com.lanou3g.bookstore.util.util.JDBCUtil;

public class UserService {
    private UserDao userDao = new UserDao();

    public String regist(User form) {
        try {
            JDBCUtil.begin();
            User user = userDao.findByUsername(form.getUsername());
            if (user != null) {
                return "用户名已被注册";
            }
            User byEmail = userDao.findByEmail(form.getEmail());
            if (byEmail != null) {
                return "Email已被注册";
            }
            userDao.add(form);
            return null;
        } finally {
            JDBCUtil.closeConn();
        }
    }

    public User login(User form) {
        try {
            JDBCUtil.begin();
            User user = userDao.findByUsername(form.getUsername());
            if (user == null) {
                //用户名不存在
                return null;
            } else {
                if (user.getPwd().equals(form.getPwd())) {
                    //登录成功
                    return user;
                } else {
                    //登录失败
                    return null;
                }
            }
        } finally {
            JDBCUtil.closeConn();
        }
    }
}
